//
//  ZH_GoodsNumTableViewCell.m
//  WisdomStar
//
//  Created by zhangliwen on 2018/11/14.
//  Copyright © 2018年 zhangliwen. All rights reserved.
//

#import "ZH_GoodsNumTableViewCell.h"
@interface ZH_GoodsNumTableViewCell()
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UIButton *addButton;
@property (nonatomic,strong)UIButton *reductionButton;
@property (nonatomic,strong)UILabel  *numLabel;
@property (nonatomic,strong)UIButton *bottomButton;
@end
@implementation ZH_GoodsNumTableViewCell

///创建
+(instancetype)cellWithTableView:(UITableView*)tableView
{
    static NSString *ID = @"ZH_GoodsNumTableViewCell";
    ZH_GoodsNumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[ZH_GoodsNumTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}
///初始化
-(void)initDefault
{
    [self unShowClickEffect];
    
}
- (void)loadSubviewConstraints{
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(FITSCALE(22));
        make.top.equalTo(self.contentView).offset(FITSCALE(10));
    }];
    [self.reductionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel);
        make.right.equalTo(self.contentView).offset(FITSCALE(-100));
        make.height.mas_equalTo(FITSCALE(25));
    }];
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.reductionButton);
        make.top.equalTo(self.reductionButton);
        make.left.equalTo(self.reductionButton.mas_right);
        make.width.mas_equalTo(FITSCALE(40));
    }];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.reductionButton);
        make.top.equalTo(self.reductionButton);
        make.left.equalTo(self.numLabel.mas_right);
    }];
    [self.bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(FITSCALE(15));
        make.left.equalTo(self.contentView).offset(FITSCALE(22));
        make.right.equalTo(self.contentView).offset(FITSCALE(-22));
        make.height.mas_equalTo(FITSCALE(40));
        make.bottom.equalTo(self.contentView).offset(FITSCALE(-15));
    }];
}
- (void)button:(UIButton *)sender{
    if (sender.tag == 1) {
        NSInteger index = [self.numLabel.text integerValue];
        if (index > 1) {
            index = index - 1;
            NSString *num = [NSString stringWithFormat:@"%ld",index];
            self.numLabel.text = num;
            if (self.onButtonClick) {
                self.onButtonClick(self.numLabel.text);
            }
        }
    }else if (sender.tag == 2){
        NSInteger index = [self.numLabel.text integerValue];
        if (index < 99) {
            index = index + 1;
            NSString *num = [NSString stringWithFormat:@"%ld",index];
            self.numLabel.text = num;
            if (self.onButtonClick) {
                self.onButtonClick(self.numLabel.text);
            }
        }
    }
    else if (sender.tag == 3){
        if (self.onButtonClicks) {
            self.onButtonClicks();
        }
    }
}
#pragma mark - getter,setter
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [UILabel createLabelWithText:@"购买数量" font:kFontSize_28];
        label.textColor = kFontColorGray;
        [self.contentView addSubview:_titleLabel = label];
    }
    return _titleLabel;
}
- (UIButton *)reductionButton{
    if (!_reductionButton) {
        UIButton *reductionButton = [[UIButton alloc] init];
        [reductionButton setTitle:@"-" forState:UIControlStateNormal];
        reductionButton.backgroundColor = [UIColor clearColor];
        [reductionButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        reductionButton.tag = 1;
        [reductionButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        reductionButton.layer.borderWidth = 1.0;
        reductionButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
        reductionButton.layer.masksToBounds = YES;
        reductionButton.clipsToBounds = YES;
        
        [self.contentView addSubview: _reductionButton = reductionButton] ;
    }
    return _reductionButton;
}
- (UILabel *)numLabel{
    if (!_numLabel) {
        UILabel *numLabel = [[UILabel alloc] init];
        numLabel.textColor = [UIColor grayColor];
        numLabel.font = [UIFont systemFontOfSize:10];
        numLabel.text = @"1";
        numLabel.textAlignment = NSTextAlignmentCenter;
        numLabel.layer.borderWidth = 1.0;
        numLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        [self.contentView addSubview: _numLabel = numLabel];
    }
    return _numLabel;
}
- (UIButton *)addButton{
    if (!_addButton) {
        UIButton *reductionButtons = [[UIButton alloc] init];
        [reductionButtons setTitle:@"+" forState:UIControlStateNormal];
        reductionButtons.backgroundColor = [UIColor clearColor];
        [reductionButtons setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        reductionButtons.layer.borderWidth = 1.0;
        reductionButtons.tag = 2;
        [reductionButtons addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        reductionButtons.layer.borderColor = [UIColor lightGrayColor].CGColor;
        reductionButtons.layer.masksToBounds = YES;
        reductionButtons.clipsToBounds = YES;
        [self.contentView addSubview:_addButton = reductionButtons];
    }
    return _addButton;
}
- (UIButton *)bottomButton{
    if (!_bottomButton) {
        UIButton *button = [UIButton createNoBgButtonWithTitle:@"确定" target:self action:@selector(button:)];
        button.backgroundColor = kFontColorRed;
        [button setTitleColor:kFontColorWhite forState:UIControlStateNormal];
        button.layer.cornerRadius = FITSCALE(18);
        button.layer.masksToBounds = YES;
        button.clipsToBounds = YES;
        button.tag = 3;
        [self.contentView addSubview:_bottomButton = button];
    }
    return _bottomButton;
}
@end
